<extend name="Layout:layout_edit" />
<block name="head_after_block">
    <style type="text/css">li {margin-top:5px;}</style>
</block>
<block name="body_content_block">
    <form action="{:AddonU('TaskStrategy/item_save')}" method="post" data-success-fn="reload_page()">
        <ul>
            <if condition="$isSuperAdmin">
                <label>租户ID：</label><select name="siteID" title="请选择租户" required data-url="{:AddonU('Admin/get_admin_json')}" data-value="{$data.siteID}"></select>
                <else/>
                <li style="display: none;"><input type="hidden" name="siteID" value="{$loginData.siteID}"/></li>
            </if>
            <li>
                <label>策略名称：</label>
                <input type="text" name="name" value="{$data.name}" required/>
            </li>
            <li>
                <label>中继名称：</label>
                <select name="trunkName" required data-url="{:AddonU('Trunk/get_json')}" data-value="{$data.trunkName}"></select>
                <input type="hidden" name="regMode" value="{$data.regMode}"/>
                <input type="hidden" name="regNumber" value="{$data.speed}" />
            </li>
            <li>
                <label>显示号码：</label>
                <input type="text" name="callerNo" title="请填写主叫号码,多个主叫号码之间用英文逗号[,]隔开" value="{$data.callerNo}"
                       placeholder="请填写主叫号码" size="45"/>
            </li>
            <li>
                <label>呼通后操作：</label><!--顺序不能变,否则配置文件会乱! 2014.6.28 by sutroon-->
                <select name='outOper' required>
                    <option>请选择操作</option>
                    <option value="0">直接转队列</option>
                    <option value="1">按码转队列</option>
                    <option value="6">放音转队列</option>
                    <option value="10">应答后外呼</option>
                    <option value="2">按码转菜单</option>
                    <option value="3">放音后挂机</option>
                    <option value="4">留言后挂机</option>
                    <option value="5">按码后挂机</option>
                    <!--
                            <option value="7">直接转队列评价</option>
                            <option value="8">按码转队列评价</option>
                            <option value="9">放音转队列评价</option>
                    -->
                </select>
                <span class="outOperDefaultQueue">
                    <input type="checkbox" name="outDefaultQueueEnable" value="1"/>放音结束后无按键转队列
                    <select name="outDefaultQueue" data-url="{:AddonU('Queue/get_queue_json')}" data-value="{$data.outDefaultQueue}"></select>
                </span>
            </li>
            <li>
                <label>队列号：</label>
                <select name="outQueue" data-url="{:AddonU('Queue/get_queue_json')}" data-value="{$data.outQueue}"></select>
            </li>
            <li>
                <label>按码设置：</label>
                <input type="text" readonly="readonly" size="40" name="outParam" value="{$data.outParam}"/>
                <input type="button" class="button" onclick="create_outOperContent(this)" value="选择">
                <a href="#" onclick="$(this).prev().prev().val('');
                        return false;">[x]</a>
            </li>
            <li>
                <label>放音时长转队列：</label>
                <input type="checkbox" name="outExp1Enable" value="1"/>
                <span style="display:none;">放音<input type="text" size="2" name="outExp1" value="{$data.outExp1}"/>(秒)转队列</span>
            </li>
            <li>
                <label>是否评价：</label>
                <input type="checkbox" name="outEcEnable" value="1"/>
                <span style="display:none;">评价语音：
                    <select name="outEcMusic" data-url="{:AddonU('Music/get_vocfile_json')}" data-value="{$data.outEcMusic}"></select> 
                    <!--评价按键：<input type="text" size="4" name="outEcKey" value="{$data.outEcKey}" />-->
                </span>
            </li>
            <li>
                <label>放音文件：</label>
                <select name="music" data-url="{:AddonU('Music/get_vocfile_json')}" data-value="{$data.music}"></select>
            </li>
            <li>
                <label>最大放音次数：</label>
                <input type="text" name="musicLimit" value="{$data.musicLimit|default='1'}" size="14"/>
            </li>
            <li>
                <label>播放工号：</label>
                <select name="sayExtension" required>
                    <option value="1" selected="selected">播放</option>
                    <option value="0">不播放</option>
                </select>
            </li>
            <li style="display: none;">
                <label>录音标识：</label>
                <select name="recState" required>
                    <option value="1" selected="selected">录音</option>
                    <option value="0">不录音</option>
                </select>
            </li>
            <li>
                <label>外呼速度：</label>
                <select name="speedMode" required>
                    <option value="0">请选择外呼速度</option>
                    <option value='percent'>座席比例</option>
                    <option value='channel'>通道数量</option>
                </select>
                <span id="speedPanel"></span>
            </li>
            <li>
                <label>呼叫超时时长：</label>
                <input type="text" name="answerExpireTime" value="{$data.answerExpireTime|default=60}" />
            </li>
            <li>
                <label>备注：</label>
                <input type="text" name="remark" value="{$data.remark}" />
            </li>
            <li style="display:none;" id="createdTime">
                <label>创建时间：</label>
                {$data.createdTime}
            </li>
        </ul>
        <input type='hidden' name="id" value='{$data.id}'/>
    </form>
    <input type="hidden" id="musicmenu_url" value="{:AddonU('MusicMenu/get_json')}" />
</block>
<block name="body_after_block">
    <script>
        item_form.resize_owner_dialog({width: 500});
        if ($('[name="id"]').val()) {
            $('input[name="name"]').attr('readonly', true);
            $('#createdTime').show();
        }
        var strQueueOpts = '';
        var strVocMenuOpts = '';
        $('select[name="speedMode"] option[value="{$data.speedMode}"]').prop('selected', 'selected');
        $('select[name="outOper"] option[value="{$data.outOper}"]').prop('selected', 'selected');
        $('select[name="sayExtension"] option[value="{$data.sayExtension}"]').prop('selected', 'selected');
        if ('{$data.outExp1Enable}' == '1') {
            $('input[name="outExp1Enable"]').prop('checked', true).next().show().parent().show();
        }
        if ('{$data.outEcEnable}' == '1') {
            $('input[name="outEcEnable"]').prop('checked', true).next().show().parent().show();
        }
        $('input[name="outEcEnable"], input[name="outExp1Enable"]').click(function () {
            $(this).next().toggle($(this).prop('checked'));
        })

        ajax_load_options({selector: 'select[name="siteID"]', def_option: '选择租户', func: afterSiteID});
        afterSiteID();
        function afterSiteID() {
            var val = $('[name="siteID"]').val();
            if (!val) {
                return;
            }
            ajax_load_options({selector: 'select[name="trunkName"]', queryParams: {siteID: val}, def_option: '请选择中继名称', func: afterTrunkName});
            ajax_load_options({selector: 'select[name="outQueue"]', queryParams: {siteID: val}, def_option: '选择队列号码', func: afterQueue});
            ajax_load_options({selector: 'select[name="outDefaultQueue"]', queryParams: {siteID: val}, def_option: '选择队列号码', func: afterQueue});
            ajax_load_options({selector: '', url: $('#musicmenu_url').val(), queryParams: {siteID: val}, def_option: '选择语音菜单', func: afterVocMenu});
            ajax_load_options({selector: 'select[name="music"]', queryParams: {type: "sounds", siteID: val}});
            ajax_load_options({selector: 'select[name="outEcMusic"]', queryParams: {type: "sounds", siteID: val}});
        }
        $('select[name="siteID"]').change(function () {
            afterSiteID();
        })

        set_trunkName('{$data.regMode}');
        function afterTrunkName() {
            set_speedMode('{$data.speedMode}');
        }
        function afterQueue(opts) {
            strQueueOpts = opts;
            set_outOper('{$data.outOper}');
            $('input[name="outParam"]').val('{$data.outParam}');
        }
        function afterVocMenu(opts) {
            strVocMenuOpts = opts;
            set_outOper('{$data.outOper}');
            $('input[name="outParam"]').val('{$data.outParam}');
        }
        // 中继号码
        $('select[name="trunkName"]').change(function () {
            var txt = $(this).find('option:selected').text();
            set_trunkName((txt.indexOf('(ip|') > 0) ? 'ip' : 'register');
            var speedMode = $('select[name="speedMode"]').val();
            if (speedMode) {
                set_speedMode(speedMode);
            }
            $('input[name="regNumber"]').val(get_trunk_reg_number());
        });
        function set_trunkName(regMode) {
            if (regMode == 'ip') {
                $('input[name="regMode"]').val('ip');
            } else {
                $('input[name="regMode"]').val('register');
            }
        }
        // 外呼速度
        $('select[name="speedMode"]').change(function () {
            var val = $(this).val();
            set_speedMode(val);
        });
        function get_trunk_reg_number() {
            var txt = $('select[name="trunkName"]').val();
            txt = $('select[name="trunkName"]').find('option[value="' + txt + '"]').text();
            var regNumber = 0; // 中继最大通道数(注册数量)
            regNumber = txt.substring(txt.indexOf('|') + 1);
            regNumber = regNumber.substring(0, regNumber.indexOf('|') + 1);
            regNumber = regNumber.substring(0, regNumber.length - 1);
            return regNumber;
        }
        function set_speedMode(val) {
            if (val == 'percent') {
                $('#speedPanel').html('1:<input type="text" name="speed" value="{$data.speed}" size="3" />');
            } else if (val == 'channel') {
                // 中继最大通道数下拉
                var str = '<select name="speed"><option value="0">请选择</option>'
                var regNumber = get_trunk_reg_number();
                if (regNumber) {
                    for (var i = 1; i <= regNumber; i++) {
                        str += '<option value="' + i + '"' + ('{$data.speed}' == i ? ' selected="selected"' : '') + '>' + i + '</option>';
                    }
                }
                $('#speedPanel').html('最大通道数:' + str);
            } else {
                $('#speedPanel').html('');
            }

        }

        // 呼通后操作
        $('select[name="outOper"]').change(function () {
            var val = $(this).val();
            set_outOper(val);
        });
        function set_outOper(val) {
            $('select[name="music"]').parent().show();
            $('input[name="musicLimit"]').parent().show();
            if (val == '') {
                return;
            }
            if (10 != val) {
                $('select[name="speedMode"]').parent().show();
                if ((',0,6,'.indexOf(',' + val + ',') > -1)) {
                    $('select[name="speedMode"]').html('<option value="0">请选择外呼速度</option><option value="percent">座席比例</option><option value="channel">通道数量</option>');
                    $('select[name="speedMode"] option[value="{$data.speedMode}"]').prop('selected', true);
                    set_speedMode('{$data.speedMode}');
                } else {
                    $('select[name="speedMode"]').html('<option value="0">请选择外呼速度</option><option value="channel" selected="selected">通道数量</option>');
                    set_speedMode('channel');
                }
            } else {
                $('select[name="speedMode"]').parent().hide();
            }
            // $('select[name="sayExtension"]').parent().toggle((',0,1,6,'.indexOf(',' + val + ',') > -1));
            $('select[name="sayExtension"], [name="answerExpireTime"], [name="callerNo"], [name="trunkName"]').parent().toggle((10 != val));
            $('input[name="musicLimit"]').parent().toggle((',1,2,3,4,5,'.indexOf(',' + val + ',') > -1));
            $('select[name="music"]').parent().toggle((',1,2,3,4,5,6,'.indexOf(',' + val + ',') > -1));
            // $('select[name="music"]').parent().toggle((',0,7,'.indexOf(',' + val + ',') > -1));
            $('input[name="outEcEnable"]').parent().toggle((',0,1,6,'.indexOf(',' + val + ',') > -1));
            // $('input[name="outExp1Enable"]').parent().toggle((',6,'.indexOf(',' + val + ',') > -1));
            $('input[name="outExp1Enable"]').parent().toggle(val == '6');
            if (val == '1') {
                if ('{$data.outDefaultQueue}') {
                    $('select[name="outOper"]').parent().find('.outOperDefaultQueue input').prop('checked', true);
                }
                $('select[name="outOper"]').parent().find('.outOperDefaultQueue').show();
            } else {
                $('select[name="outOper"]').parent().find('.outOperDefaultQueue').hide();
            }
            $('input[name="outParam"]').val('').parent().toggle((',1,2,'.indexOf(',' + val + ',') > -1));
            $('select[name="outQueue"]').parent().toggle((',0,6,7,10,'.indexOf(',' + val + ',') > -1));
        }
        function create_outOperContent(osrc) {
            var options = '';
            var val = $('select[name="outOper"]').val();
            var type = '';
            if ((',1,6,8,9,'.indexOf(',' + val + ',') > -1)) {
                type = 'queue';
            }
            if ((',2,'.indexOf(',' + val + ',') > -1)) {
                type = 'menu';
            }
            if (type == '') {
                return;
            }
            switch (type) {
                case 'queue':
                    options = strQueueOpts;
                    break;
                case 'menu':
                    options = strVocMenuOpts;
                    break;
            }
            var params = $(osrc).prev().val();
            var str = '<div style="width:500px; height:300px; display:table;" title="设置" id="dlg-outoper"><ul>';
            var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'x'];
            for (var i = 0; i < data.length; i++) {
                str += '<li style="float:left; margin:5px 10px 0px 0px;">' + data[i] + '键<select class="outOperContent-picker" data-id="' + data[i] + '">' + select_optParam_dialog(data[i], params, options) + '</select>' + ((i > 0 && (i + 1) % 3 < 1) ? '<br />' : '') + '</li>';
            }
            str += '</ul></div>';
            $(str).dialog({
                width: 450,
                height: 300,
                buttons: [
                    {text: "关闭", handler: function () {
                            var $dlg = $('#dlg-outoper').dialog("close").parent();
                            $dlg.next().remove();
                            $dlg.remove();
                        }}
                ]
            });
            $('#dlg-outoper').find('select').change(function () {
                var val = $(this).val();
                var id = $(this).attr('data-id');
                var $input = $(osrc).prev();
                var str = $input.val();
                $input.val(replace_value(str, id, isNaN(val) ? 'REMOVE' : val));
            });
        }
        /**
         * 正则选择符合指定值的Option标签
         * @param {string} str options字符串
         * @param {mixed} val 值
         * @returns {string} 返回包含selected="selected"的options字符串
         * @example set_option_selected(opts, params);
         * @since 1.0 2014-6-27 by sutroon
         */
        function set_option_selected(str, val) {
            if (!val || !str)
                return str;
            var pattern = eval('/=["|\']?' + val + '["|\']?/g');
            str = str.replace(pattern, '="' + val + '" selected="selected"');
            return str;
        }
        function select_optParam_dialog(id, params, opts) {
            if (!params)
                return opts;
            if (params.indexOf(':') < 0) {
                return set_option_selected(opts, params);
            }
            var arr1 = params.split(';');
            var arr2;
            for (var i = 0; i < arr1.length; i++) {
                arr2 = arr1[i].split(':');
                if (arr2[0] == id) {
                    opts = set_option_selected(opts, arr2[1]);
                    break;
                }
            }
            return opts;
        }

        /**
         * 替换键值
         * @param {string} str
         * @param {mixed} id
         * @param {mixed} val
         * @returns {string}
         * @since 1.0 2014-6-26 by sutroon
         */
        function replace_value(str, id, val) {
            if (str.length > 0) {
                str = ';' + str;
                if (str.indexOf(';' + id + ':') > -1) {
                    var pattern = eval('/;' + id + ':\\d+[^;]?/g'); // 坑X!!!by sutroon
                    if (val == 'REMOVE') {
                        str = str.replace(pattern, '');
                    } else {
                        str = str.replace(pattern, ';' + id + ':' + val);
                    }
                } else {
                    str += ';' + id + ':' + val;
                }
                str = str.substring(1);
            } else {
                str = id + ':' + val;
            }
            return str;
        }
        function reload_page(){
            window.parent.window.location.reload();
        }
    </script>
</block>